package com.zb.controller;

import com.zb.entity.NewsDetail;
import com.zb.service.NewsDetailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;

import java.util.Date;
import java.util.List;

/**
 * Created by 11965 on 2019/7/8.
 */
@Controller
public class NewsDetailController {

    @Autowired
    private NewsDetailService newsDetailService;

        @GetMapping("ei221")
    public String showNewsDetail(Model model){
        List<NewsDetail> list = newsDetailService.listAll();
        model.addAttribute("list",list);
        return "index";
    }

    @GetMapping("toAdd")
    public String toAdd(){
        return "add";
    }
    @PostMapping("add")
    public String add(NewsDetail n){
        n.setCreatedate(new Date());
        newsDetailService.addNews(n);
        return "redirect:showNewsDetail";
    }

    @GetMapping("delete")
    public String delete(int id,Model model){
        int flag = newsDetailService.deleteNews(id);
        model.addAttribute("deleteflag",flag);
        return "forward:showNewsDetail";
    }

    @GetMapping("toUpdate/{id}")
    public String toUpdate(@PathVariable("id") int id,Model model){
        NewsDetail n = newsDetailService.findById(id);
        model.addAttribute("news",n);
        return "update";
    }


    @PostMapping("update")
    public String update(NewsDetail n){
        n.setCreatedate(new Date());
        int flag = newsDetailService.updateNews(n);
        return "redirect:showNewsDetail";
    }
}
